/*************************************************************************
	> File Name: servesudo.c
	> Author: anhao
	> Mail: wzzlyzdn@qq.com 
	> Created Time: 2020年08月27日 星期四 08时04分21秒
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<pthread.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<time.h>

#define USER_NUM 10

int creat_socket();
int accept_client(int sev_socket);
void *server_client_communite(void *arg);

pthread_t thread[USER_NUM], exit_pth;

typedef struct 
{
    int client_socket;
    char name[20];
}USER_INFO;

USER_INFO usrinfo_t[USER_NUM] = {0};
char buf_sudo[100] = "请输入姓名！\n";

int print_t(char *buf)
{
    time_t t = 0;
    struct tm *tt = NULL;
    time(&t);
    tt = localtime(&t);
    sprintf(buf, "%02d-%02d %02d:%02d:%02d\n", 
            (tt->tm_mon+1), 
            tt->tm_mday, tt->tm_hour, 
            tt->tm_min, tt->tm_sec
            );
    return 0;
}

int main()
{
    int sev_socket = creat_socket();
    int client_socket = -1;
    printf("服务器启动成功！\n");
    while(1)
    {
        client_socket = accept_client(sev_socket);
        printf("---------------------\n");
    }
    
    for(int i = 0; i < USER_NUM; i++)
        if(usrinfo_t[i].client_socket != 0)
                close(usrinfo_t[i].client_socket);
    close(sev_socket);
    //pthread_join(thread, NULL);
    return 0;
}


int creat_socket()
{
    int sev_socket = -1;
    sev_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sev_socket)
    {
        perror("socket 创建失败！\n");
        return -1;
    }
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr_in));
    
    addr.sin_family = AF_INET;
    addr.sin_port = 8080;
    addr.sin_addr.s_addr = 0;
    
    int reuse = 1;
    int ret = -1;
    ret = setsockopt(sev_socket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

    int flag_bind = -1;
    flag_bind = bind(sev_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr));
    if(-1 == flag_bind)
    {
        perror("绑定端口失败！\n");
        return -1;
    }

    int flag_listen = -1;
    flag_listen = listen(sev_socket, 10);
    if(-1 == flag_listen)
    {
        perror("监听失败！\n");
        return -1;
    }
    return sev_socket;
}

int accept_client(int sev_socket)
{   

    struct sockaddr_in client_addr;
    memset(&client_addr, 0, sizeof(struct sockaddr_in));
    int addrlen = sizeof(client_addr);
    printf("等待连接客户机！\n");
    
    int client_socket = accept(sev_socket, (struct sockaddr *)&client_addr, &addrlen);
    if(-1 == client_socket)
    {
        perror("连接客户机失败!\n");
        return -1;
    }
    int num = 0;
    for(int i = 0; i < USER_NUM; i++)
    {
        if(usrinfo_t[i].client_socket == 0)
        {
            long arg = (long)i;
            usrinfo_t[i].client_socket = client_socket;
            pthread_create(thread + i, NULL, server_client_communite, (void *)arg);
            pthread_detach(thread[i]);
            break;
        }
    }
    printf("接入客户机：%s\n", inet_ntoa(client_addr.sin_addr));
    return client_socket;
}

void *server_client_communite(void *arg)
{
    long num = (long)arg;
    int rev_sev = 0;
    while(1)
    {
        char buf[100]={0};
        char buf1[1000] = {0};
        char ui[30] = "----------------\n";
        if(usrinfo_t[num].client_socket)
        {     
            if(strlen(usrinfo_t[num].name) == 0)
            {
                send(usrinfo_t[num].client_socket, buf_sudo, sizeof(buf_sudo), 0);
                rev_sev = recv(usrinfo_t[num].client_socket, buf, 100, 0);
                buf[rev_sev] = '\0';
                sprintf(usrinfo_t[num].name, "$%s", buf);
                sprintf(buf1, "%s已上线！", usrinfo_t[num].name);
                printf("%s\n", buf1);
                for(int j = 0; j < USER_NUM; j++)
                {
                    if(j != num)
                    {
                            send(usrinfo_t[j].client_socket, buf1, strlen(buf1), 0);
                    }
                }
                    continue;
            }
            rev_sev = recv(usrinfo_t[num].client_socket, buf, 100, 0);
            buf[rev_sev] = '\0';
                      
                
            if(rev_sev > 0)
            {
                char time[100] = {0};
                print_t(time);
                printf("接受来自客户机%s的消息：%s \n", usrinfo_t[num].name, buf);
                sprintf(buf1, "%s%s : %s", time, usrinfo_t[num].name, buf);
                for(int j = 0; j < USER_NUM; j++)
                {
                    if(j != num)
                    {
                        send(usrinfo_t[j].client_socket, ui, strlen(ui), 0);
                        send(usrinfo_t[j].client_socket, buf1, strlen(buf1), 0);
                        //send(usrinfo_t[j].client_socket, ui, strlen(ui), 0);
                    }
                }
            }

            else
                {
                    printf("客户机%s断开连接！\n", usrinfo_t[num].name);
                    usrinfo_t[num].client_socket = 0;
                    memset(&usrinfo_t[num].name, '\0', sizeof(usrinfo_t[num].name));
                    pthread_exit(NULL);
                    break;
                }
            if(strncmp(buf, "quit", 4) == 0)
            {
                printf("用户%s登出！\n", usrinfo_t[num].name);
                usrinfo_t[num].client_socket = 0;
                memset(&usrinfo_t[num].name, '\0', sizeof(usrinfo_t[num].name));
                pthread_exit(NULL);       
                break;
            }
        }
    }
    pthread_exit(NULL);
}
